const gulp = require('gulp');
const versionRev = require('gulp-version-rev');

const DEFAULT_OPTIONS = {
        '.ftl': [
          {reg: /<script.*?\s+src="(.+?\.js)"(\s+|>)/g, pos: 1},
          {reg: /<script.*?\s+src="(<@ofbizContentUrl>.+?\.js<\/@ofbizContentUrl>)"(\s+|>)/g, pos: 1},
          {reg: /<link.*?\s+href="(.+?\.css)"(\s+|(\/)?>)/g, pos: 1},
          {reg: /<link.*?\s+href="(<@ofbizContentUrl>.+?\.css<\/@ofbizContentUrl>)"(\s+|(\/)?>)/g, pos: 1},
          {reg: /<img.*?\s+src="(.+?)"(\s+|\/>|>)/g, pos: 1},
          {reg:  /url\(["'](?!data:image)(.+?)["']?\)/g, pos: 1}
        ],
        '.html': [
          {reg: /<script.*?\s+src="(.+?\.js)"(\s+|>)/g, pos: 1},
          {reg: /<link.*?\s+href="(.+?\.css)"(\s+|(\/)?>)/g, pos: 1},
          {reg: /<img.*?\s+src="(.+?)"(\s+|\/>|>)/g, pos: 1}
        ],
        '.css': [
        	{reg:  /url\(["']?(?!data:image)(.+?)["']?\)/g, pos: 1}
        ],
        ignoreUrls: [],
        preprocess: {
          '.ftl': [/\$\{.+?}/g]
        },
        version: new Date().getTime()
};

// copy 会在build之前完成
gulp.task('build', function () {
    return gulp.src(['../src/main/resources/webapp/**/*.ftl','../src/main/resources/webapp/**/*.css'])
        .pipe(versionRev(DEFAULT_OPTIONS))
        .pipe(gulp.dest('../src/main/resources/webapp'));
});